package com.alison.datastream;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * @Author alison
 * @Date 2024/4/3 18:19
 * @Version 1.0
 * @Description
 */
public class SourceDataStream {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
//        DataStreamSource<String> inputDataStream = fromSocketData(env);
        DataStreamSource<String> inputDataStream = fromReadTextFile(env);
        //基于数据流进行计算
        DataStream<Tuple2<String, Integer>> resultStream = inputDataStream.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public void flatMap(String value, Collector<Tuple2<String, Integer>> out) throws Exception {
                String[] words = value.split("[\\s,\\.]");
                for (String word : words) {
                    out.collect(new Tuple2<>(word, 1));
                }
            }
        }).keyBy(0).sum(1);
        resultStream.print();
        env.execute("socket source");
    }

    public static DataStreamSource<String> fromSocketData(StreamExecutionEnvironment env) {
        //从socket中获取数据流数据
        DataStreamSource<String> source = env.socketTextStream("192.168.56.101", 9999);
//        DataStreamSource<String> csvEnv = env.readTextFile("file://E:/data/(sample)user_result.csv");
        return source;
    }

    public static DataStreamSource<String> fromReadTextFile(StreamExecutionEnvironment env) {
        //从socket中获取数据流数据
//        DataStreamSource<String> source = env.socketTextStream("localhost", 9999);
        DataStreamSource<String> source = env.readTextFile("E://data//(sample)user_result.csv");
        return source;
    }
}
